<?php

namespace App\Admin\Controllers;

use App;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\DeveloperLink;
use App\Models\TestingDeveloper;
use DeveloperLinkService;
use TestingDeveloperService;

class DeveloperLinkController extends Controller
{
    use ModelForm;

    public function __construct()
    {
        $this->developerOption = TestingDeveloper::all()->pluck('name','union_code');
        $this->positionOption = DeveloperLinkService::getPositionOption();
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_developer_link.title.list'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('backend.testing_developer_link.title.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_developer_link.title.add'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(DeveloperLink::class, function (Grid $grid) {
            $grid->union_code(trans('backend.testing_developer_link.column.union_code'))->display(function($unionCode){
                $data = TestingDeveloperService::getByUnionCode($unionCode)->name;
                return $data;
            });
            $grid->name(trans('backend.testing_developer_link.column.name'));
            $grid->position(trans('backend.testing_developer_link.column.position'))->display(function($postion){
                $name = '';
                if($postion == 1){
                    $name = '微信端';
                }else if($postion == 2){
                    $name = 'app端';
                }else{
                    $name = 'web端';
                }
                return $name;
            });
            $grid->url(trans('backend.testing_developer_link.column.url'));

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->useModal();
                $filter->is('union_code',trans('backend.testing_developer_link.column.union_code'))->select($this->developerOption);
                $filter->is('position',trans('backend.testing_developer_link.column.position'))->select($this->positionOption);
            });

            $grid->disableExport();

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(DeveloperLink::class, function (Form $form) {
            $form->select("union_code",trans('backend.testing_developer_link.column.union_code'))->options($this->developerOption)->rules('required');
            $form->select("position",trans('backend.testing_developer_link.column.position'))->options($this->positionOption)->rules('required');
            $form->text("name",trans('backend.testing_developer_link.column.name'))->rules('required');
            $form->url("url",trans('backend.testing_developer_link.column.url'))->rules('required');
        });
    }

    public function options()
    {
        return DeveloperLinkService::getResultOptions();
    }
}
